package com.wanter.vote.controller;

import com.wanter.common.utils.result.ReturnMap;
import com.wanter.vote.entity.BeVoted;
import com.wanter.vote.entity.Vote;
import com.wanter.vote.service.BeVotedService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 被投信息实体类
 * Created by hp on 2017/5/19.
 */
@Controller
@RequestMapping("/beVotedAction/")
public class BeVotedController {
    private final Logger logger = Logger.getLogger(this.getClass());

    @Resource
    private BeVotedService beVotedService;


    /**
     * 增加一个投票信息
     *
     * @param beVoted 被投信息
     * @return 结果集
     */
    @RequestMapping(method = RequestMethod.POST, value = "addBeVoted")
    @ResponseBody
    public ReturnMap addBeVoted(BeVoted beVoted) {
        ReturnMap returnMap = new ReturnMap();
        if (StringUtils.isEmpty(beVoted.getAudUSer().getId())) {
            returnMap.setMessage("缺少上传用户");
            returnMap.setSuccess(false);
            return returnMap;
        }
        if (StringUtils.isEmpty(beVoted.getName())) {
            returnMap.setMessage("缺少标题");
            returnMap.setSuccess(false);
            return returnMap;
        }
        if (StringUtils.isEmpty(beVoted.getDesc())) {
            returnMap.setMessage("缺少描述信息");
            returnMap.setSuccess(false);
            return returnMap;
        }
        if (StringUtils.isEmpty(beVoted.getVoteimage())) {
            returnMap.setMessage("缺少图片信息");
            returnMap.setSuccess(false);
            return returnMap;
        }
        beVotedService.insertBeVoted(beVoted);
        if (logger.isInfoEnabled()) {
            logger.info("新增被投信息成功" + beVoted);
        }
        returnMap.setMessage("新增成功");
        return returnMap;
    }

    /**
     * 投赞同票
     *
     * @param id 被投id
     * @return 结果集
     */
    @RequestMapping(method = RequestMethod.POST, value = "agreeBeVoted")
    @ResponseBody
    public ReturnMap agreeBeVoted(int id, Vote vote) {
        ReturnMap returnMap = new ReturnMap();
        if (StringUtils.isEmpty(id) && StringUtils.isEmpty(vote.getVoteuserid())) {
            returnMap.setMessage("id为空或者用户的id为空");
            returnMap.setSuccess(false);
        }
        beVotedService.updateBeVoteNum(id, vote);
        if (logger.isInfoEnabled()) {
            logger.info("id=" + id + "投了赞同票");
        }
        returnMap.setMessage("投票成功");
        return returnMap;
    }

    /**
     * 投反对票
     *
     * @param id 被投id
     * @return 结果集
     */
    @RequestMapping(method = RequestMethod.POST, value = "disAgreeBeVoted")
    @ResponseBody
    public ReturnMap disAgreeBeVoted(int id, Vote vote) {
        ReturnMap returnMap = new ReturnMap();
        if (StringUtils.isEmpty(id) && StringUtils.isEmpty(vote.getVoteuserid())) {
            returnMap.setMessage("id为空或者用户的id为空");
            returnMap.setSuccess(false);
        }
        beVotedService.updateBeVotePeos(id, vote);
        if (logger.isInfoEnabled()) {
            logger.info("id=" + id + "投了反对票");
        }
        return returnMap;
    }
}
